IF EXISTS (SELECT * FROM sysobjects WHERE type = 'FN' AND name = 'RemoveTime')
BEGIN
	PRINT 'Dropping function RemoveTime'
	DROP  FUNCTION dbo.RemoveTime
END
GO

PRINT 'Creating function RemoveTime'
GO

CREATE FUNCTION dbo.RemoveTime (@dt datetime)
RETURNS smalldatetime
AS
BEGIN
	DECLARE @ret SMALLDATETIME
	SET @ret = NULL
	IF @dt IS NOT NULL
	BEGIN
		IF @dt < '1900-01-01'
			SET @dt = '1900-01-01'

		IF @dt > '2076-06-05'
			SET @dt = '2076-06-05'

		SET @dt = DATEADD(ms,(-1)*DATEPART(ms,@dt),@dt)
		SET @dt = DATEADD(ss,(-1)*DATEPART(ss,@dt),@dt)
		SET @dt = DATEADD(mi,(-1)*DATEPART(mi,@dt),@dt)
		SET @dt = DATEADD(hh,(-1)*DATEPART(hh,@dt),@dt)

		SET @ret = CAST(@dt AS smalldatetime) 
	END

	RETURN  @ret
END
GO
